{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2b3feabb",
   "metadata": {},
   "source": [
    "# 什么是稀疏数据\n",
    "稀疏数据是指那些大部分没有使用的元素（不携带任何信息的元素）的数据。\n",
    "\n",
    "它可以是一个像这样的数组。\n",
    "\n",
    "[1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0]\n",
    "\n",
    "稀疏数据：是一个数据集，其中大部分项目的值为零。\n",
    "\n",
    "密集数组：是稀疏数组的反面：大部分数值不为零。\n",
    "\n",
    "在科学计算中，当我们在线性代数中处理偏导时，我们会遇到稀疏数据。\n",
    "\n",
    "# 如何处理稀疏数据\n",
    "SciPy有一个模块，scipy.sparse，提供了处理稀疏数据的函数。\n",
    "\n",
    "我们主要使用两种类型的稀疏矩阵。\n",
    "\n",
    "CSC - 压缩稀疏列。用于高效的算术，快速的列切分。\n",
    "\n",
    "CSR - 压缩稀疏行。用于快速的行切分，更快的矩阵向量乘积。\n",
    "\n",
    "我们将在本教程中使用CSR矩阵。\n",
    "\n",
    "# CSR矩阵\n",
    "我们可以通过向函数scipy.sparse.csr_matrix()传递一个array来创建CSR矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bfc1db5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.sparse import csr_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b00fc048",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (0, 5)\t1\n",
      "  (0, 6)\t1\n",
      "  (0, 8)\t2\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2])\n",
    "print(csr_matrix(arr))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfbc687f",
   "metadata": {},
   "source": [
    "# 稀疏矩阵方法\n",
    "用数据属性查看存储的数据（不是零项）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e1cf442a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 2]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[0, 0, 0], [0, 0, 1], [1, 0, 2]])\n",
    "print(csr_matrix(arr).data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "735bb93f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# 用count_nonzero()方法计算非零的数量。\n",
    "print(csr_matrix(arr).count_nonzero())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "62b38a13",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (1, 2)\t1\n",
      "  (2, 0)\t1\n",
      "  (2, 2)\t2\n"
     ]
    }
   ],
   "source": [
    "# 用 eliminate_zeros()方法去除矩阵中的零条目。\n",
    "mat = csr_matrix(arr)\n",
    "mat.eliminate_zeros()\n",
    "\n",
    "print(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e8c46d15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (1, 2)\t1\n",
      "  (2, 0)\t1\n",
      "  (2, 2)\t2\n"
     ]
    }
   ],
   "source": [
    "# 用sum_duplicates()方法消除重复的条目。\n",
    "mat = csr_matrix(arr)\n",
    "mat.sum_duplicates()\n",
    "\n",
    "print(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34019ba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用tocsc()方法从csr转换到csc。\n",
    "newarr = csr_matrix(arr).tocsc()\n",
    "\n",
    "print(newarr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
